package com.tongtech.proxy.fw;

public class ConnectIpSecCheck {

    public static void main(String[] args) {
        System.out.println(isIpAddressInSubnet("192.167.2.100", "192.168.2.0/8"));
    }
    public static boolean isIpAddressInSubnet(String ipAddress, String subnetRange) {
        String[] networkips = ipAddress.split("\\.");
        int ipAddr = (Integer.parseInt(networkips[0]) << 24)
                | (Integer.parseInt(networkips[1]) << 16)
                | (Integer.parseInt(networkips[2]) << 8)
                | Integer.parseInt(networkips[3]);

        // 拿到主机数
        int type = Integer.parseInt(subnetRange.replaceAll(".*/", ""));
        int ipCount = 0xFFFFFFFF << (32 - type);

        String maskIp = subnetRange.replaceAll("/.*", "");
        String[] maskIps = maskIp.split("\\.");

        int cidrIpAddr = (Integer.parseInt(maskIps[0]) << 24)
                | (Integer.parseInt(maskIps[1]) << 16)
                | (Integer.parseInt(maskIps[2]) << 8)
                | Integer.parseInt(maskIps[3]);

        return (ipAddr & ipCount) == (cidrIpAddr & ipCount);
    }
}
